import React, { Component } from 'react'
import { connect } from 'dva';
import Link from 'umi/link';
import { Form } from 'antd';
import { SearchRow, SearchCol, SearchButton } from '@/styled/form';

import { DICT } from '../enum';

interface IProps {
  current: number,
  form: any,
  dispatch?: Function,
  initCurrent: Function,
  new: boolean,
  dict?: any,
  cond?: any
}

@connect(({ fireroom }) => ({
  dict: fireroom.dict,
  cond: fireroom.cond
}))
class Search extends Component<IProps, any> {

  componentDidMount() {
    const { dispatch } = this.props;

    dispatch({ type: 'fireroom/queryDict', payload: { keyList: DICT } }).then(() => {
      const { cond, form } = this.props;
      const { pageNum, pageSize, ...searchParams } = cond;
      form.setFieldsValue(searchParams);
    });
  }

  search = e => {
    e.preventDefault();
    const { dispatch, form, initCurrent } = this.props;
    form.validateFields((err, values) => {
      if (!err) {
        dispatch({
          type: 'fireroom/updateCond',
          payload: {
            ...values
          }
        })

        initCurrent()
      }
    });
  }

  render() {

    return (
      <Form onSubmit={this.search}>
        <SearchRow>
          <SearchCol key={3} span={24}>
            <SearchRow type="flex" justify="end" >
              <Link to='/data/fireroom/new' style={{marginLeft: '10px'}}>
                <SearchButton type="primary">新增</SearchButton>
              </Link>
            </SearchRow>
          </SearchCol>
        </SearchRow>
      </Form>
    );
  }
}

const SearchS = Form.create<IProps>()(Search);
export default SearchS;
